import Ember from 'ember';
import moment from 'moment';

export default Ember.Service.extend({
  // moment: Ember.inject.service(),

  // 将 Date 转化为指定格式的String
  // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
  // 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  // 例子：
  // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
  dateFormat: function(date,fmt){
    var o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  //取得当前时间戳
  getCurrentTime: function () {
    return Math.floor(Date.now() / 1000);
  },
  //取得某天的最后一秒的时间戳,参数是日期字符串
  getLastSecondStampOfDayString: function (dateString) {
    console.log('time string',dateString);
    var timeString = dateString + " 23:59:59";
    var time = moment(timeString,"YYYY-MM-DD hh:mm:ss");
    return time.valueOf()/1000;
  },
  getLastSecondStampOfDay: function (date) {
    console.log("date day:",date);
    var dateString = this.dateFormat(date,"yyyy-MM-dd");
    return this.getLastSecondStampOfDayString(dateString);
  },
  //取得某天的第一秒的时间戳,参数是日期字符串
  getFirstSecondStampOfDayString: function (dateString) {
    console.log('time string',dateString);
    var timeString = dateString + " 00:00:00";
    var time = moment(timeString,"YYYY-MM-DD hh:mm:ss");
    return time.valueOf()/1000;
  },
  getFirstSecondStampOfDay: function (date) {
    console.log("date day:",date);
    var dateString = this.dateFormat(date,"yyyy-MM-dd");
    return this.getFirstSecondStampOfDayString(dateString);
  },
  //时间转时间戳
  timeToTimestamp: function (time) {
    console.log('time endTime a time',time);
  	return time.getTime()/1000;
  },
  //字符串转时间戳
  timeStringToTimestamp: function (timeStr) {
    var time = Date.parse(timeStr);
    return time/1000;
  },
  //格式化时间,时间戳转字符串
  formatDate: function(timestamp,formatString) {
  	var date = this.timestampToTime(timestamp);
  	return this.dateFormat(date,formatString);
  },
  //时间戳转时间
  timestampToTime: function (timestamp) {
    var offset = new Date().getTimezoneOffset() * 60;
  	var realstamp = timestamp + 480 * 60 + offset;
  	var date = new Date(parseInt(realstamp) * 1000);
  	return date;
  },
  //时间戳转moment时间
  timestampToMoment: function (timestamp) {
    var offset = new Date().getTimezoneOffset() * 60;
  	var realstamp = timestamp + 480 * 60 + offset;
  	return moment(parseInt(realstamp) * 1000);
  },
  //格式化时间,时间戳转格式化的字符串
  datetimeToString: function (timestamp,formatString) {
    var date = this.timestampToTime(timestamp);
  	return this.formatDate(date,formatString);
  },
  //距离当前时间的时间间隔（单位秒）
  getSecondsFromNow: function(unixTimestamp){
    // var moment = this.get("moment");
    var momentRel = moment(unixTimestamp*1000);
    console.log("unixTimestamp:" + unixTimestamp);
    console.log("momentRel:",momentRel);
    console.log("dif is:" + momentRel.diff(moment(), 'seconds'));
    return moment().diff(momentRel, 'seconds');
  },
  //取得当天0点0分0秒的时间戳
  getTodayTimestamp: function(){
    var today = new Date();
    return this.getFirstSecondStampOfDay(today);
  },
  //取得几天前0点0分0秒的时间戳
  getDaysBeforeTimestamp: function(beforeNumber){
    var today = new Date();
    var rnumber = beforeNumber * -1;
    var date = moment().add(rnumber, 'day').toDate();
    return this.getFirstSecondStampOfDay(date);
  }
});
